<?php

namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Request;
session_start();

class Blog extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
    	$id = $_SESSION['id'];
        $result = Db::name('blog_name')->where('user_id', $id)->select();
        if ($result) {
            //获取文章信息 标题 作者  日期
            $list = Db::name('article')->alias('a')->field('a.id,title,author,post_time,category')->join('user', 'a.user_id=user.id')->where('user.id', $id)->select();
            return view('blog/index', ['title' => '博客管理_博客云', 'list' => $list]);
        } else {


            return view('blog/u', ['title' => '博客管理_博客云', 'id' => $id]);
        }
       // return view('blog/index', ['title' => '博客管理_博客云']);
    }

//     public function index()
//     {

// //       $result = Db::name('blog_name')->where('user_id',$id)->select();
// //       if ($result){
// //           return view('blog/index',['title'=>'博客管理_博客云']);
// //       } else {
// //          return view('blog/u',['title'=>'博客管理_博客云','id'=>$id]);
// //        }

//         return view('blog/index', ['title' => '博客管理_博客云','list'=>'']);
//     }


    //增加博客名
    public function addBlogName()
    {
        $p = input('post.');
        
        $data = [
            'name' => $p['name'],
            'domain' => $p['domain'],
            'user_id' => $p['user_id']
        ];
        $row = Db::name('blog_name')->data($data)->insert();
        if ($row > 0) {
            return $this->success('恭喜您,现在可以去写博客啦', url('index/index/index'));
        } else {
            return $this->success('添加失败,请重试');
        }
    }
    //显示博客名
    public function showBlogName(){
        $id = $_SESSION['id'];
        $list = Db::name('blog_name')->where('user_id' , $id)->select();
        if ($list > 0) {
            $info['status'] = true;
            $info['name'] = $list['name'];
            $Info['damain'] = $list['domain'];
        } 
        return json($info);
       }

    public function category()
    {
        return view('blog/blog_category', ['title' => '分类_博客云']);
    }

    public function comment()
    {
        return view('blog/blog_comment', ['title' => '评论_博客云']);
    }

    public function tag()
    {
        return view('blog/blog_tag', ['title' => '标签_博客云']);
    }

    public function link()
    {
    	$id = 1;
    	$list = Db::name('link')->where('user_id' , $id)->select();
        return view('blog/blog_link', ['list' => $list,'title'=>'友情链接_博客云']);
    }
    //增加友情链接
    public function addLink(){
         
    	$p = input('post.');
    	$data = [
    		'linkname' => $p['linkname'],
    		'url' => $p['url']
    	];
    	$list = Db::name('link')->data($data)->insert();
    	if ($list > 0) {
    		$info['status'] = true;
            $info['msg'] = '成功';
    
    	}else{
    		$info['status'] = false;
            $info['msg'] = '删除失败,请重试';
    	}
       return json(13);
    }
    //删除友情链接
    public function dellink($id){
    	 // $id = input('get.');   
    	//$id = Request::instance()->get();
    	
    	$result = Db::name('link')->delete($id);

    	if ($result > 0) {
    		$info['status'] = true;
    		$info['msg'] = '成功删除';
    	} else{
    		$info['status'] = false;
    		$info['msg'] = '删除失败,请重试';
    	}
    	return json($info);
    }
    // public function delete($id)
    // {
    //     $result = Db::name('link')->delete($id);

    //     if ($result > 0) {
    //         $info['status'] = true;
           
    //         $info['msg'] = '成功删除';
    //     } else {
    //         $info['status'] = false;
           
    //         $info['msg'] = '删除失败,请重试';
    //     }

    //     return json($info);
    // }

    //博客设置
    public function set()
    {
        $list = Db::name('blog_name')->where('user_id',$_SESSION['id'])->find();

        return view('blog/blog_set', ['title' => '设置_博客云','list'=>$list]);
    }
    //更改blog_name
    public function editBlogName(){
        $p = input('post.');
        $data['name'] = $p['name'];
        $data['intro'] = $p['description'];
        $data['pwd'] = $p['accessPwd'];
        $row = Db::name('blog_name')->where('user_id',$_SESSION['id'])->update($data);
        if ($row > 0) {
            return $this->success('更新成功' );
        }
    }

    public function count()
    {
        return view('blog/blog_vistcount', ['title' => '访客统计_博客云']);
    }

    public function allblog()
    {
        return view('blog/allblog', ['title' => '全部博客_博客云']);
    }

    public function newblog()
    {
        return view('blog/newblog', ['title' => '写新日志_博客云']);
    }

    //删除文章
    public function deleteArt($id)
    {
        dump($id);die;
        $row = Db::name('article')->delete($id);
        if ($row > 0) {
        	$info['status'] = true;
        	$info['msg'] = '文章已经被删除';
        } else{
        	$info['status'] = false;
        	$info['msg'] = '删除失败,请重试';
        }
        return json($info);
    }

    //增加文章
    public function addArt(Request $request)
       {	
       		$p = input('post.');
            //var_dump($p);
       	    $data = [
                'user_id' => $_SESSION['id'],
                'title' =>$p['title'],
                'author' => $_SESSION['name'],
                'type' => $p['type'],
                'tags' => $p['tags'],
                'content'=>$p['content'],
                'post_time' => $p['post_time'],
                'allow_recommend' => empty($p['allow_recommend'])?0:1,
                'always_top' => empty($p['always_top'])?0:1,
                'allow_comment' => empty($p['allow_comment'])?0:1,
                'allow_trackback' => empty($p['allow_trackback'])?0:1
            ];
            //var_dump($data);
            $row = Db::name('article')->data($data)->insert();
            if ($row >0) {
               //return view('index/article',['list'=>$data,'title'=>'用户文章']);
                return $this->success('恭喜您,发布成功' ,'index');
            } else{
                echo '加载失败';
            }
       }
   


}